import React from 'react';
import base from './util/base';
import { _,WfForm_Public,GlobalUtil } from './util/pcPublic';
import { toJS } from 'mobx';
import { Bubble,Tools } from 'weaver-mobile';
import { showConfirm,showHoverWindow } from './util/common';
import * as Util from './util/formUtil';
import* as SubmitUtil from './util/submit';
import { doRightMenuClick } from './util/formBtnUtil';
import { SIGN_CONTENT } from './constants/variableKey';
import { doSignInputChange } from './util/SignUtil';
import CellType from './components/layout/CellType';

export const WfForm_Mobile = {
    getOperateStore:function(){
        return base.wfOperateStore;
    },
    getSignInputStore:function(){
        return base.allStores.wfSignInputStore;
    },
    showMessage: function(msg, type=1, duration=1.5, style={}){
        if(type === 1)      //警告
            Bubble.warning(msg, duration, style);
        else if(type === 2)     //错误
            Bubble.error(msg, duration, style);
        else if(type === 3)     //成功
            Bubble.success(msg, duration, style);
        else    //普通信息
            Bubble.remind(msg, duration, style);
    },
    showConfirm: function(content, okEvent, cancelEvent, otherInfo={}){
        const hasCancel = otherInfo.otherInfo === false ? false : true;
        const obj = {...otherInfo, ...{title:React.createElement("div", {dangerouslySetInnerHTML:{__html:content}}), okEvent, cancelEvent}};
        showConfirm(obj, hasCancel);
    },
    getSignRemark: function(){
        const { variableMap } = base.allStores.wfSignInputStore;
        return  variableMap.has(SIGN_CONTENT) ? toJS(variableMap.get(SIGN_CONTENT)) : "";
    },
    /**
     * 设置签字意见
     * @param {value} 要插入的内容
     * @param {position} 要插入的位置
     * @param {isClear} 默认为false替换意见内容，true追加意见内容
     * @param {callback} 插入成功后的回调函数
     */
    setSignRemark: function(value, isClear = true, isAfter = true, callback){
        const {markInfo = {}} = base.wfOperateStore;
        const {extendParams = {}} =toJS(markInfo);
        const {mobileSignInputRichText = false} = extendParams;
        const { variableMap, ckStore } = base.allStores.wfSignInputStore;
        let remark  = variableMap.has(SIGN_CONTENT) ? toJS(variableMap.get(SIGN_CONTENT)) : "";
        if(isClear){
            remark = value;
        }else{
            if(isAfter){
                remark = remark + value;
            }else{
                remark = value + remark;
            }
        }
        let updateParams = {};
        updateParams[SIGN_CONTENT] = remark;
        doSignInputChange(updateParams, updateParams);
        if(mobileSignInputRichText){//用ck
            ckStore.setData(remark);
        }
        if(typeof callback==="function"){
            callback();
        }
    },
    reloadPage: function(params={}){
        const { requestid } = base.wfGlobalStore.commonParam;
        const newParams = {...{requestid}, ...params};
        Util.reloadRequestPage(newParams);
    },
    controlBtnDisabled: function(isDisabled){
        base.wfOperateStore.controllBottomBtnDisable(isDisabled);
    },
    doRightBtnEvent: function(arg1){
        try{
            const rightMenu = toJS(base.wfOperateStore.rightMenu.rightMenus);
            let btnObj;
            rightMenu && rightMenu.map(item =>{
                if(typeof arg1 === "string" && arg1 === item.type)
                    btnObj = item;
                else if(typeof arg1 === "object" && arg1.type === item.type && arg1.menuFun === item.menuFun)
                    btnObj = item;
            });
            if(btnObj !== null)
                doRightMenuClick(btnObj);
        }catch(e){
        }
    },
    downLoadFile: function(fileid, route="/req"){
        const { commonParam } = base.wfGlobalStore;
        const { requestid,authStr,authSignatureStr,f_weaver_belongto_userid,f_weaver_belongto_usertype } = commonParam;
        let url = "/weaver/weaver.file.FileDownload?fileid="+fileid+"&download=1"+"&fromrequest=1";
        url += `&requestid=${requestid}&authStr=${authStr}&authSignatureStr=${authSignatureStr}&f_weaver_belongto_userid=${f_weaver_belongto_userid}&f_weaver_belongto_usertype=${f_weaver_belongto_usertype}`;
        //showHoverWindow(url, route);
        Tools.getWeaverFile(url);
    },
    generateFieldContentComp: function(fieldMark){
        const index = fieldMark.indexOf("_");
        const fieldid = index>-1 ? fieldMark.substring(5, index) : fieldMark.substring(5);
        const rowIndex = index>-1 ? fieldMark.substring(index+1) : -1;
        const fieldInfo = WfForm_Public.getFieldInfo(fieldid);
        const symbol = fieldInfo.tableMark || "";
        if(symbol !== ""){
            const detailProps = symbol.indexOf("detail") > -1 ? {rowIndex} : {};
            const cellObj = {id:"0,0", etype:"3", field:fieldid};
            return <CellType symbol={symbol} cellObj={cellObj} detailProps={detailProps} />
        }
        return <span>Field Not Exist</span>;
    },
    appendSubmitParam: function(obj={}){
        _.forIn(obj, (v,k) =>{
            if(_.indexOf(GlobalUtil.importantParamRange, k) > -1)
                delete obj[k];
        });
        base.wfOperateStore.changeSubmitParam(obj);
    },
    getFirstRequiredEmptyField: function(){
        return SubmitUtil.verifyRequiredEmptyField();
    },
    verifyFormRequired: function(mustAddDetail=true, fieldRequired=true){
        let flag = true;
        //校验并提示，必须新增空明细
        if(mustAddDetail){
            flag = SubmitUtil.verifyMustAddDetail();
        }
        //校验并提升，第一个必填字段
        if(fieldRequired && flag){
            const emptyField = this.getFirstRequiredEmptyField();
            if (emptyField !== "") {
                flag = false;
                window.setTimeout(()=>{
                    Util.showPrompt(emptyField, 1);     //明细可能需要切页，延时提示
                },50);
            }
        }
        return flag;
    },
    getLocation: function() {
        const { locationInfo } = base.allStores.wfLocationMap;
        return toJS(locationInfo);
    },
    switchTab:function(key,type=1){
        Util.switchTabFun(key,type);
    },
    /*将单选框设置为radio显示 */
    changeToRadio:function(fieldMark,bool){
        let wfLayoutStore = base.allStores.wfLayoutStore;
        const {controlVariableMap} = wfLayoutStore;
        controlVariableMap("control_radio_" + fieldMark,bool);
    },
    /*将复选框设置为checkbox显示 */
    changeToCheckbox:function(fieldMark,bool){
        let wfLayoutStore = base.allStores.wfLayoutStore;
        const {controlVariableMap} = wfLayoutStore;
        controlVariableMap("control_checkbox_" + fieldMark,bool);
    }
}

const WfForm = {...WfForm_Public, ...WfForm_Mobile};

window.WfForm = WfForm;
window.wfform = WfForm;
window.toJS = toJS;